package com.douwong.student.activity;

import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.support.v7.app.AlertDialog;
import android.text.InputType;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;

import com.douwong.student.R;
import com.douwong.student.application.BaseApplication;
import com.douwong.student.base.BaseActivity;
import com.douwong.student.contant.Config;
import com.douwong.student.interfaces.ItemListener;
import com.douwong.student.popuwindow.AppVersionUpdate;
import com.douwong.student.utils.AppInfoUtils;
import com.douwong.student.utils.NetUtils;
import com.douwong.student.utils.PreferencesUtils;
import com.douwong.student.utils.ToastUtils;
import com.douwong.student.view.PersonInfoItemView;

/**
 * @autor yichao
 * @time 2017/3/289:23
 * @des ${TODO}
 */
public class SetActivity extends BaseActivity implements View.OnClickListener {
    private PersonInfoItemView mItemViewNetState, mItemViewSound, mItemViewPatriarch, mItemViewVersionUpgragde, mItemViewLookLog, mItemViewExitLogin;
    private ImageView mImageBack;
    private AppVersionUpdate mAppVersionUpdate;

    @Override
    protected void initView() {
        setContentView(R.layout.activity_set_system);
        mItemViewNetState = (PersonInfoItemView) findViewById(R.id.itemview_net_state);
        mItemViewSound = (PersonInfoItemView) findViewById(R.id.itemview_sound);
        mItemViewPatriarch = (PersonInfoItemView) findViewById(R.id.itemview_patriarch);
        mItemViewVersionUpgragde = (PersonInfoItemView) findViewById(R.id.itemview_version_upgrade);
        mItemViewLookLog = (PersonInfoItemView) findViewById(R.id.itemview_look_log);
        mItemViewExitLogin = (PersonInfoItemView) findViewById(R.id.itemview_exit_login);
        mImageBack = (ImageView) findViewById(R.id.go_back_img);
    }

    @Override
    protected void initData() {
        super.initData();
        mItemViewNetState.setViewName(NetUtils.getlocalip(this) + "");
        mItemViewSound.setImageViesible(View.VISIBLE);
        boolean isRound = PreferencesUtils.getInstance().getBoolean(Config.IS_ROUND, true);
        mItemViewSound.setImageResoure(isRound ? R.mipmap.generl_btn_on : R.mipmap.generl_btn_off);
        //家长模式
        mItemViewPatriarch.setImageViesible(View.VISIBLE);
        boolean isPatriarch = PreferencesUtils.getInstance().getBoolean(Config.IS_PATRIARCH, false);
        mItemViewPatriarch.setImageResoure(isPatriarch ? R.mipmap.generl_btn_on : R.mipmap.generl_btn_off);
        mItemViewVersionUpgragde.setViewName(AppInfoUtils.getVersionName(this));
        mItemViewExitLogin.setVisibility(BaseApplication.isLogin ? View.VISIBLE : View.GONE);
        mItemViewExitLogin.setViewTitleColor(Color.RED);

    }

    @Override
    protected void initEvent() {
        super.initEvent();
        mImageBack.setOnClickListener(this);
        mItemViewVersionUpgragde.setOnClickListener(this);
        mItemViewLookLog.setOnClickListener(this);
        mItemViewExitLogin.setOnClickListener(this);

        //音效
        mItemViewSound.setImagListener(new ItemListener() {
            @Override
            public void onImagClick(View view) {
                boolean Round = PreferencesUtils.getInstance().getBoolean(Config.IS_ROUND, true);
                if (Round) {
                    mItemViewSound.setImageResoure(R.mipmap.generl_btn_off);
                    PreferencesUtils.getInstance().putBoolean(Config.IS_ROUND, false);
                } else {
                    mItemViewSound.setImageResoure(R.mipmap.generl_btn_on);
                    PreferencesUtils.getInstance().putBoolean(Config.IS_ROUND, true);
                }

            }
        });
        //家长模式
        mItemViewPatriarch.setImagListener(new ItemListener() {
            @Override
            public void onImagClick(View view) {
                boolean patriarch = PreferencesUtils.getInstance().getBoolean(Config.IS_PATRIARCH, true);
                if (patriarch) {//开启。关闭
                    mItemViewPatriarch.setImageResoure(R.mipmap.generl_btn_off);
                    PreferencesUtils.getInstance().putBoolean(Config.IS_PATRIARCH, false);
                    //家长模式关闭
                    Intent turnOffIntent = new Intent();
                    turnOffIntent.setAction(Config.TURN_OFF_PATRIARCH);
                    sendBroadcast(turnOffIntent);

                } else {
                    //关闭、开启
                    final EditText input = new EditText(SetActivity.this);
                    input.setHeight(100);
                    input.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
                    final AlertDialog.Builder builde = new AlertDialog.Builder(SetActivity.this);
                    builde.setTitle("输入密码").setIcon(android.R.drawable.ic_dialog_info).setView(input)
                            .setNegativeButton("取消", null);
                    builde.setPositiveButton("确定", new DialogInterface.OnClickListener() {

                        public void onClick(DialogInterface dialog, int which) {
                            String sec = input.getText().toString();

                            String password = PreferencesUtils.getInstance().getString(Config.FAMILY_KEY_PWD, "");
                            if (!password.equals("")) {
                                BaseApplication.AppKeyPWD = password;
                            }
                            if (BaseApplication.AppKeyPWD.equals(sec.trim())) {

                                mItemViewPatriarch.setImageResoure(R.mipmap.generl_btn_on);
                                PreferencesUtils.getInstance().putBoolean(Config.IS_PATRIARCH, true);
                                //家长模式打开
                                Intent turnOnIntent = new Intent();
                                turnOnIntent.setAction(Config.TURN_ON_PATRIARCH);
                                sendBroadcast(turnOnIntent);

                            } else {
                                ToastUtils.makeText(SetActivity.this, "密码错误！");

                            }
                        }
                    });

                    builde.show();


                }

            }
        });


    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.go_back_img:
                finish();
                break;
            case R.id.itemview_version_upgrade:
                //版本升级
                mAppVersionUpdate = new AppVersionUpdate(this, v);
                mAppVersionUpdate.checkVersionUpdate(null, true);
                break;
            case R.id.itemview_look_log:
                //查看日志
                break;
            case R.id.itemview_exit_login:
                //注销登录
                //退出登录
                if (BaseApplication.isLoginClass){
                    ToastUtils.makeText(this,"正在上课，不能退出课堂");
                    return;
                }
                new AlertDialog.Builder(this).setTitle("提示").setMessage("退出现在登录的用户吗？")
                        .setNegativeButton("取消", null)
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                unResignStatue();
                                dialog.dismiss();
                                finish();
                            }
                        }).create().show();

                break;
        }
    }

    /**
     * 注销状态
     */
    private void unResignStatue() {
        BaseApplication.isLogin=false;
        BaseApplication.sUserBean=null;
        //发送注销登录的广播
        Intent intent=new Intent();
        intent.setAction(Config.UNREGISTER_LOGIN);
        sendBroadcast(intent);
    }
}
